//app.js

App({
  onLaunch: function() {

    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    });

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  initBody: function() {
    return new Promise((resolve, reject) => {
      const fileManage = wx.getFileSystemManager();
      var that = this;
      var filePath = that.globalData.bodyDataPath;
      console.log('filePath: ');
      console.log(filePath);

      fileManage.access({
        path: filePath,
        success: res => {
          console.log("静默body数据文件已存在");
          console.log(res);
          resolve(true);
        },
        fail: res => {
          console.log("静默body数据文件不存在");
          console.log(res);
          // 创建文件以及静默数据格式
          fileManage.writeFile({
            filePath: filePath,
            data: JSON.stringify(that.globalData.body),
            encoding: 'utf8',
            success: res => {
              console.log('静默body数据文件创建成功');
              resolve(true);
            },
            fali: res => {
              console.log('静默body数据文件创建失败');
              reject(false);
            }
          });
        }
      });
    });
  },
  globalData: {
    userInfo: null,
    bodyDataPath: wx.env.USER_DATA_PATH + '/body.json',
    bodyItems: [{
        flag: 1,
        text: '身高',
        icon: '../image/1.png',
        unit: 'cm',
        attribute: 'hight',
      },
      {
        flag: 2,
        text: '体重',
        icon: '../image/2.png',
        unit: 'kg',
        attribute: 'weight',
      },
      {
        flag: 3,
        text: '胸围',
        icon: '../image/3.png',
        unit: 'cm',
        attribute: 'circumference',
      },
      {
        flag: 4,
        text: '腰围',
        icon: '../image/4.png',
        unit: 'cm',
        attribute: 'waistline',
      },
      {
        flag: 5,
        text: '臀围',
        icon: '../image/5.png',
        unit: 'cm',
        attribute: 'hipline',
      },
    ],
    // body data
    body: {
      hight: {
        text: '身高',
        icon: '../image/1.png',
        unit: 'cm',
        data: [
          {
            time: "2019-08-05",
            value: 178
          }, 
          {
          time: "2019-08-03",
          value: 160
          }, 
          {
            time: "2019-08-02",
            value: 165
          }
        ]
      },
      weight: {
        text: '体重',
        icon: '../image/2.png',
        unit: 'kg',
        data: [
          {
            time: "2019-08-05",
            value: 65.5
          },
          {
            time: "2019-08-03",
            value: 63.5
          },
          {
            time: "2019-08-02",
            value: 62.5
          },
          {
          time: "2019-08-01",
          value: 61.5
          },
        ]
      },
      circumference: {
        text: '胸围',
        icon: '../image/3.png',
        unit: 'cm',
        data: [{
          time: "2019-08-03",
          value: 95
        }]
      },
      waistline: {
        text: '腰围',
        icon: '../image/4.png',
        unit: 'cm',
        data: [{
          time: "2019-08-03",
          value: 100
        }]
      },
      hipline: {
        text: '臀围',
        icon: '../image/5.png',
        unit: 'cm',
        data: [{
          time: "2019-08-03",
          value: 90
        }]
      },
    }
  }
})